package com.xt.xtblesdk6;

import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.RequestCallback;
import com.xthh.XTCore;
import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._enum.HH_EnumErrorCode;
import com.xthh._enum.HH_EnumFileType;
import com.xthh._function.HH_CharUtil;
import com.xthh._function.HH_Logging;
import com.xthh._function.HH_PicUtils;
import com.xthh._function.HH_SystemUtil;
import com.xthh._function.HH_TimeUtil;
import com.xthh._function.HH_ToastUtils;
import com.xthh._function._ini.HH_INI;
import com.xthh._msg.HH_MSG;
import com.xthh._struct.HH_T_BindDevice;
import com.xthh._struct.HH_T_ScanResult;
import com.xthh._struct.HH_T_SystemParams;
import com.xthh.picselect.HH_Activity_PicFileSelect;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final String TAG = "MainActivity";
    Context ctx;
    Button button_ota;
    Button button_scan;
    Button button_brushpic;
    Button button_nfc_close;
    Button button_ver_hard;
    Button button_nfc_bind;
    Button button_brushpic_cancel;
    ImageView imageview_pic;
    TextView textView_w;
    TextView textView_h;
    TextView textView_color;
    TextView textView_mac;
    TextView textView_number;

    TextView textView_color_s;
    TextView textView_sn;
    TextView textView_srrc;
    EditText editText_stop_time;

    private NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;
    NFCStateReceiver nfcStateReceiver;
    ScreenStatusReceiver mScreenStatusReceiver;
    BluetoothStateReceiver bluetoothStateReceiver;
    ProgressBar progressba_brushpic;

    // 身份认证信息不能更改
    final String strDeviceIdentity = "0000000000";
    final int NFC_BIND_STATE = 102;
    final int MSG_RE_BIND = 701;
    final int MSG_OTHER_LOG = 702;

    String testMac = "31391801D776";


    public static HandlenRecvMessage handlenRecvMessage;


    int deviceW = 800;
    int deviceH = 480;
    int deviceNumber = 113;
    HH_EnumDeviceColorType deviceColorType = HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3;
    List<Integer> listSendData = new ArrayList<>();


    private AdapterCurrentTaskLogInfo mAdapterCurrentTaskLogInfo;
    ListView list_log_info;

    HH_T_SystemParams systemParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = this;
        progressba_brushpic = findViewById(R.id.progressba_brushpic);

        imageview_pic = findViewById(R.id.imageview_pic);
        imageview_pic.setOnClickListener(this);

        button_brushpic = findViewById(R.id.button_brushpic);
        button_brushpic.setOnClickListener(this);

        button_ota = findViewById(R.id.button_ota);
        button_ota.setOnClickListener(this);
        button_nfc_bind = findViewById(R.id.button_nfc_bind);
        button_nfc_bind.setOnClickListener(this);

        button_brushpic_cancel = findViewById(R.id.button_brushpic_cancel);
        button_brushpic_cancel.setOnClickListener(this);

        button_ver_hard = findViewById(R.id.button_ver_hard);
        button_ver_hard.setOnClickListener(this);

        button_nfc_close = findViewById(R.id.button_nfc_close);
        button_nfc_close.setOnClickListener(this);

        textView_w = findViewById(R.id.textView_w);
        textView_h = findViewById(R.id.textView_h);
        textView_color = findViewById(R.id.textView_color);
        textView_mac = findViewById(R.id.textView_mac);
        textView_number = findViewById(R.id.textView_number);

        button_scan = findViewById(R.id.button_scan);
        button_scan.setOnClickListener(this);

        //HH_Logging.i(TAG, "版本号：%d", lib_ver);


        textView_color_s = findViewById(R.id.textView_color_s);
        textView_sn = findViewById(R.id.textView_sn);
        textView_srrc = findViewById(R.id.textView_srrc);



        handlenRecvMessage = new HandlenRecvMessage();

        systemParams = new HH_T_SystemParams(getApplication(), ctx, handlenRecvMessage);


        list_log_info = findViewById(R.id.list_log_info);
        mAdapterCurrentTaskLogInfo = new AdapterCurrentTaskLogInfo(this);
        list_log_info.setAdapter(mAdapterCurrentTaskLogInfo);
        AddLogInfo(String.format("lib版本号=%d", XTCore.GetLibVer()));

        deviceW = HH_INI.getInt(systemParams, "DEVICE_W");
        deviceH = HH_INI.getInt(systemParams, "DEVICE_H");
        deviceNumber = HH_INI.getInt(systemParams, "DEVICE_NUMBER");
        testMac = HH_INI.getString(systemParams, "DEVICE_MAC");
        deviceColorType = HH_INI.getDeviceColor(systemParams, "DEVICE_COLOR");
        if (deviceW == 0 || deviceH == 0){
            deviceW = 528;
            deviceH = 768;
            testMac = "31391801DBE9";
            deviceNumber = 113;
            deviceColorType = HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3;

            HH_INI.putInt(systemParams, "DEVICE_W", deviceW);
            HH_INI.putInt(systemParams, "DEVICE_H", deviceH);
            HH_INI.putInt(systemParams, "DEVICE_NUMBER", deviceNumber);
            HH_INI.putString(systemParams, "DEVICE_MAC", testMac);
            HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", deviceColorType);
        }

        // 测试，强度改为16色
        //deviceColorType = HH_EnumDeviceColorType.N_COLOR_16_6;

        textView_w.setText(String.format("%d", deviceW));
        textView_h.setText(String.format("%d", deviceH));
        textView_number.setText(String.format("%d", deviceNumber));
        textView_mac.setText(testMac);
        textView_color.setText(String.format("%d", deviceColorType.ordinal()));


        editText_stop_time = findViewById(R.id.editText_stop_time);
        editText_stop_time.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && s.toString() != null){
                    int time = HH_CharUtil.Atoi(s.toString());
                    if (time > 0)
                        HH_INI.putInt(systemParams, "STOP_TIME_13", time);
                }
            }
        });

        int time = HH_INI.getInt(systemParams, "STOP_TIME_13");
        if (time <= 0){
            time = 1;
            HH_INI.putInt(systemParams, "STOP_TIME_13", time);
        }
        editText_stop_time.setText(String.format("%d", time));


        if (nfcStateReceiver == null) {
            nfcStateReceiver = new NFCStateReceiver();
        }
        IntentFilter filter = new IntentFilter();
        IntentFilter nfcFilter = new IntentFilter();
        filter.addAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
        registerReceiver(nfcStateReceiver, filter);


        if (mScreenStatusReceiver == null){
            mScreenStatusReceiver = new ScreenStatusReceiver();
        }
        IntentFilter screenStatusIF = new IntentFilter();
        screenStatusIF.addAction(Intent.ACTION_SCREEN_ON);
        screenStatusIF.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mScreenStatusReceiver, screenStatusIF);


        if (bluetoothStateReceiver == null) {
            bluetoothStateReceiver = new BluetoothStateReceiver();
        }
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(bluetoothStateReceiver, filter);


        OpenPerm();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(nfcStateReceiver);
        unregisterReceiver(mScreenStatusReceiver);
    }

    @Override
    protected void onStart() {
        super.onStart();

    }



    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    protected void onPause() {
        super.onPause();

    }


    private void OpenPerm() {
        PermissionX.init(this)
                .permissions(
                        Manifest.permission.BLUETOOTH,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        //Manifest.permission.MANAGE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA)
                .explainReasonBeforeRequest()
                .onExplainRequestReason((scope, deniedList, beforeRequest) -> scope.showRequestReasonDialog(deniedList,
                        "即将申请的权限是程序必须的权限",
                        "确定",
                        "取消"))
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
                        if (allGranted) {
                            initApp();
                        } else {
                            //requestPermission();
                        }
                    }
                });
    }

    private void initApp() {



    }


    private void LoadLocalPic(String src) {
        Bitmap b1 = HH_PicUtils.GetBitmapFormFile(src);
        if (b1 != null) {

            if (b1.getWidth() != deviceW || b1.getHeight() != deviceH) {

                //b1 = HH_PicUtils.scaleBitmap(b1, deviceW, deviceH);
                b1 = HH_PicUtils.zoomImg2(b1, deviceW, deviceH, 255, 255, 255);

            }
            if (b1 != null) {

                // 测试的图需要旋转
                b1 = HH_PicUtils.rotateBitmap(b1, 270);

                Long timeBegin = HH_TimeUtil.MadeMillisTimeMS();
                b1 = XTCore.PicDither(b1, deviceColorType, true, 1);
                HH_Logging.i(TAG, "抖点耗时：%d,分辨率=%d*%d", HH_TimeUtil.MadeMillisTimeMS() - timeBegin, b1.getWidth(), b1.getHeight());

                if (b1 != null) {
                    listSendData = new ArrayList<>();
                    listSendData = XTCore.GetSendPicData(b1, deviceColorType);
                    HH_Logging.i(TAG, "%d", listSendData.size());

                    // 临时写入文件
                    String tempPath = HH_SystemUtil.GetTempPath(systemParams, 0, HH_EnumFileType.BMP);
                    HH_PicUtils.BitmapSaveFile(b1, tempPath);
                    Bitmap b2 = HH_PicUtils.GetBitmapFormFile(tempPath);
                    b2 = HH_PicUtils.rotateBitmap(b2, 90);

                    imageview_pic.setImageBitmap(b2);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_nfc_bind:{
                Intent intent = new Intent(this, NFCBindActivity2.class);
                startActivityForResult(intent, NFC_BIND_STATE);

                break;
            }
            case R.id.button_ver_hard:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_Logging.i(TAG, systemParams,"固件版本号：%s", XTCore.GetHardVersion(systemParams, testMac));
                            AddLogInfo(String.format("固件版本号：%s", XTCore.GetHardVersion(systemParams, testMac)));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_scan:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_Logging.i(TAG, "scan-begin");
                            ArrayList<HH_T_ScanResult> list = XTCore.Scan(systemParams, testMac);
                            if (list != null){
                                for (int i = 0; i < list.size(); i++){
                                    if (list.get(i).getBindDevice() != null){
                                        HH_Logging.i(TAG, "mac=%s,rssi=%d", list.get(i).getBindDevice().getMacRes(), list.get(i).getBindDevice().getDbm());

                                        deviceW = list.get(i).getBindDevice().getWidth();
                                        deviceH = list.get(i).getBindDevice().getHeight();
                                        deviceColorType = list.get(i).getBindDevice().getDeviceColorType();

                                        HH_INI.putInt(systemParams, "DEVICE_W", deviceW);
                                        HH_INI.putInt(systemParams, "DEVICE_H", deviceH);
                                        HH_INI.putString(systemParams, "DEVICE_MAC", list.get(i).getBindDevice().getMacRes());
                                        HH_INI.putDeviceColor(systemParams, "DEVICE_COLOR", deviceColorType);
                                    }
                                }
                            }
                            HH_Logging.i(TAG, "scan-end");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_brushpic_cancel:{
                AddLogInfo("主动取消");
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = NFCClose(testMac);
                            HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());
                            HH_SystemUtil.SendMessage(systemParams, MSG_OTHER_LOG, code.toString(), 0, 0);
                            XTCore.StopBrushPic();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_brushpic:{
                if (listSendData == null || listSendData.size() <= 0){
                    HH_ToastUtils.showToast(ctx, "先选图");
                    break;
                }

                if (deviceNumber != 113){
                    AddLogInfo("设备不匹配");
                    break;
                }

                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = BrushPic(testMac, listSendData);
                            HH_Logging.i(TAG, systemParams, "XTCore.BrushPic = %s", code.toString());
                            if (code == HH_EnumErrorCode.BLE_CONNECT_FAIL){
                                //HH_SystemUtil.SendMessage(systemParams, MSG_RE_BIND, "", 0, 0);
                            } else if (code == HH_EnumErrorCode.FAIL){
                                HH_SystemUtil.SendMessage(systemParams, MSG_OTHER_LOG, "传输失败", 0, 0);
                            } else if (code == HH_EnumErrorCode.BRUSH_FAIL){
                                HH_SystemUtil.SendMessage(systemParams, MSG_OTHER_LOG, "刷图失败", 0, 0);
                            }
                        } catch (Exception e) {
                            HH_Logging.i(TAG, "brush-end fail = %s", e.toString());
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.imageview_pic:{
                Intent intent = new Intent(this, HH_Activity_PicFileSelect.class);
                ArrayList<String> listExts = new ArrayList<String>();
                listExts.add("png");
                listExts.add("jpg");
                listExts.add("jpeg");
                listExts.add("bmp");
                intent.putStringArrayListExtra("FIND_FILE_EXT", listExts);
                startActivityForResult(intent, HH_MSG.XT_FILE_SELECT_PIC);
                break;
            }
            case R.id.button_nfc_close:{
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = NFCClose(testMac);
                            HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());
                            HH_SystemUtil.SendMessage(systemParams, MSG_OTHER_LOG, code.toString(), 0, 0);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            }
            case R.id.button_ota:{
                Intent intent = new Intent(this, HH_Activity_PicFileSelect.class);
                ArrayList<String> listExts = new ArrayList<String>();
                listExts.add("bin");
                intent.putStringArrayListExtra("FIND_FILE_EXT", listExts);
                startActivityForResult(intent, HH_MSG.XT_FILE_SELECT_OTA);
                break;
            }
        }
    }

    private HH_EnumErrorCode BrushPic(String mac, List<Integer> listData) {
        if (listData == null || listData.size() <= 0)
            return HH_EnumErrorCode.DATA_FAIL;

        int time = 1;
        if (editText_stop_time.getText() == null || editText_stop_time.getText().toString() == null){
            time = 1;
        } else {
            time = HH_CharUtil.Atoi(editText_stop_time.getText().toString());
        }
        HH_Logging.i(TAG, "超时间=%d", time);
        return XTCore.BrushPic(systemParams, mac, listData, 10, true, strDeviceIdentity, time);
    }

    private HH_EnumErrorCode NFCClose(String mac) {
        return XTCore.NFCClose(systemParams, mac, strDeviceIdentity);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == HH_MSG.XT_FILE_SELECT_PIC){
                if (data != null) {
                    String path = data.getStringExtra("IMPORT_PATH_FILE");
                    LoadLocalPic(path);
                }
            } else if (requestCode == HH_MSG.XT_FILE_SELECT_OTA){
                if (data != null) {
                    String path = data.getStringExtra("IMPORT_PATH_FILE");
                    OTA(path);
                }
            } else if (requestCode == NFC_BIND_STATE){

                deviceNumber = HH_INI.getInt(systemParams, "DEVICE_NUMBER");

                deviceW = HH_INI.getInt(systemParams, "DEVICE_W");
                deviceH = HH_INI.getInt(systemParams, "DEVICE_H");
                testMac = HH_INI.getString(systemParams, "DEVICE_MAC");
                deviceColorType = HH_INI.getDeviceColor(systemParams, "DEVICE_COLOR");
                textView_w.setText(String.format("%d", deviceW));
                textView_h.setText(String.format("%d", deviceH));
                textView_number.setText(String.format("%d", deviceNumber));
                textView_mac.setText(testMac);
                textView_color.setText(String.format("%d", deviceColorType.ordinal()));

                textView_color_s.setText(HH_INI.getString(systemParams, "DEVICE_COLOR_S"));
                textView_sn.setText(HH_INI.getString(systemParams, "DEVICE_SN"));
                textView_srrc.setText(HH_INI.getString(systemParams, "DEVICE_SRRC"));

                AddLogInfo("绑定成功");
            }
        }
    }

    private void AddLogInfo(String info){
        if (info != null && info.length() > 0){

            mAdapterCurrentTaskLogInfo.addTaskInfo(info);
            mAdapterCurrentTaskLogInfo.notifyDataSetChanged();
            if (mAdapterCurrentTaskLogInfo.getCount() > 0)
                list_log_info.setSelection(mAdapterCurrentTaskLogInfo.getCount() - 1);
        }
    }

    private void OTA(String path) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    HH_EnumErrorCode code = XTCore.OTA(systemParams, testMac, path, 10, strDeviceIdentity);
                    HH_SystemUtil.SendMessage(systemParams, MSG_OTHER_LOG, code.toString(), 0, 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 去掉自动绑定

    }

    //NFC状态监听
    private class NFCStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                NfcManager manager = (NfcManager) ctx.getSystemService(Context.NFC_SERVICE);
                NfcAdapter adapter = manager.getDefaultAdapter();
                if (adapter != null) {
                    if (!adapter.isEnabled()){
                        AddLogInfo("NFC未开启");
                    }else {
                        AddLogInfo("NFC已开启");
                    }
                }
            } catch (Exception e) {
                AddLogInfo("NFC未开启");
            }
        }
    }

    // 监听各种状态
    public class HandlenRecvMessage extends Handler {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case MSG_RE_BIND:{
                    Intent intent = new Intent(ctx, NFCBindActivity.class);
                    startActivityForResult(intent, NFC_BIND_STATE);
                    break;
                }
                case MSG_OTHER_LOG:{
                    AddLogInfo((String)msg.obj);
                    break;
                }
                case XTCore.MSG_PROGRESS:{

                    progressba_brushpic.setMax(msg.arg2);
                    progressba_brushpic.setProgress(msg.arg1 + 1);
                    // 进度值从0开始
                    //AddLogInfo(String.format("数据传输进度%d/%d", msg.arg1, msg.arg2));
                    break;
                }
                case XTCore.MSG_LOG:{
                    HH_EnumErrorCode errorCode = HH_EnumErrorCode.values()[msg.arg2];
                    switch (errorCode) {
                        case DATA_FAIL:
                            AddLogInfo("数据错误");
                            break;
                        case BLE_CONNECT_FAIL:
                            AddLogInfo("连接超时");
                            break;
                        case BLE_CHARACTERISTIC_FAIL:
                            AddLogInfo("特征获取失败");
                            break;
                        case CLEAR_CACHE_FAIL:
                            AddLogInfo("发送清空缓存指令失败");
                            break;
                        case SCAN_FINISH:
                            AddLogInfo("扫描完成");
                            break;
                        case BLE_BEGIN_CONNECT:
                            AddLogInfo("蓝牙开始连接");
                            break;
                        case BLE_DISABLE:
                            AddLogInfo("蓝牙没有打开");
                            break;
                        case BLE_BRUSH_BEGIN:
                            AddLogInfo("刷图开始");
                            break;
                        case BLE_BRUSH_DATA_SEND:
                            AddLogInfo("数据传输中");
                            break;
                        case BLE_NFC_CLOSE_BEGIN:
                            AddLogInfo("NFC断电开始");
                            break;
                        case BLE_OTA_BEGIN:
                            AddLogInfo("OTA开始");
                            break;
                        case SCAN_FAIL:
                            AddLogInfo("扫描失败");
                            break;
                        case SCAN_BEGIN:
                            AddLogInfo("扫描开始");
                            break;
                        case SCAN_INFO: {
                            HH_T_BindDevice tbd = (HH_T_BindDevice) msg.obj;
                            if (tbd != null)
                                AddLogInfo(tbd.getMacRes());
                            break;
                        }
                        case SUCCESS:
                            AddLogInfo("成功");
                            break;
                        case FAIL:
                            AddLogInfo("失败");
                            break;
                        case BLE_CONNECT_SUCCESS:
                            AddLogInfo("蓝牙连接成功");
                            break;
                        case BLE_BRUSH_ING:
                            AddLogInfo("正在刷图");
                            break;
                        case BLE_DISCONNECT:
                            AddLogInfo("断开连接");
                            break;
                        case BLE_IDENTITY_ENCRYPTION_BEGIN:
                            AddLogInfo("身份加密开始");
                            break;
                        case BLE_IDENTITY_ENCRYPTION_FAIL:
                            AddLogInfo("认证失败");
                            break;

                        case TIME_CONSUME_BLE_CONNECT:{
                            AddLogInfo(String.format("连接用时：%d", msg.arg1));
                            break;
                        }
                        case TIME_CONSUME_DATA_SEND:{
                            AddLogInfo(String.format("数据发送用时：%d", msg.arg1));
                            break;
                        }
                        case TIME_CONSUME_BRUSH:{
                            AddLogInfo(String.format("刷图用时：%d", msg.arg1));
                            break;
                        }
                    }
                    break;
                }

            }

        }

    }

    class ScreenStatusReceiver extends BroadcastReceiver {
        String SCREEN_ON = "android.intent.action.SCREEN_ON";
        String SCREEN_OFF = "android.intent.action.SCREEN_OFF";
        @Override
        public void onReceive(Context context, Intent intent) {
            if (SCREEN_ON.equals(intent.getAction())) {

            } else if (SCREEN_OFF.equals(intent.getAction())) {
                AddLogInfo("主动取消");
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HH_EnumErrorCode code = NFCClose(testMac);
                            HH_Logging.i(TAG, systemParams, "XTCore.NFCClose = %s", code.toString());
                            XTCore.StopBrushPic();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    }

    public class BluetoothStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_ON:
                        // 蓝牙已经打开
                        AddLogInfo("蓝牙已经打开");
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        // 蓝牙已经关闭
                        AddLogInfo("蓝牙已经关闭");
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        // 蓝牙正在打开
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        // 蓝牙正在关闭
                        break;
                }
            }
        }
    }
}